﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BasePlatformCommon.devChain
{
	public class ControlNode
	{
		public int type = -1;							// 设备类型：1-分控, 0-附属设备

		public int mySubCtrlDepth = 0;					// 分控级联深度

		public int myEquipmentDepth = 0;				// 附属设备级联深度

		public string version = "";						// 版本字符串

		public byte[] replyData = new byte[256];		// 读 0x0 地址反馈的数据，256byte

		public List<ControlNode> brother = null;		// 等位设备节点列表

		public int brotherCount = 0;					// 等位设备节点数量

        public bool noContinueFlag = false;				// 设备序号不连续标记

        public int subType = 0;							// 分控类型：1-分控, 0-附属设备

		// 构造函数
		public ControlNode(int type)
		{
			this.type = type;
			brother = new List<ControlNode>();
		}

		// ToString
		public override string ToString()
		{
			return String.Format("mySubCtrlDepth = {0}, myEquipmentDepth = {1}, version = {2}", mySubCtrlDepth, myEquipmentDepth, version);
		}

		// 复制节点
		public static ControlNode Clone(ControlNode src, bool withBrother)
		{
			ControlNode dst = new ControlNode(src.type);

			dst.mySubCtrlDepth = src.mySubCtrlDepth;
			dst.myEquipmentDepth = src.myEquipmentDepth;
			dst.version = src.version;
			
			Array.Copy(src.replyData, 0, dst.replyData, 0, dst.replyData.Length);

			dst.noContinueFlag = src.noContinueFlag;

			dst.brotherCount = src.brotherCount;
			if (withBrother == true)
			{
				for (int k = 0; k < src.brother.Count; k++)
				{
					ControlNode b = new ControlNode(src.type);
					b.mySubCtrlDepth = src.mySubCtrlDepth;
					b.myEquipmentDepth = src.myEquipmentDepth;
					b.version = src.version;
					dst.brother.Add(b);
				}
			}

			return dst;
		}

        public static string getSubTypeString(int subType) {
            var typeStr = "Sub";
            switch (subType) {
                case 2:
                    typeStr = "SubD";
                    break;
                case 4:
                    typeStr = "SubDW";
                    break;
                case 8:
                    typeStr = "SubD16";
                    break;
                default:
                    typeStr = "Sub";
                    break;
            }
            return typeStr;
        }
	}
}
